package io.github.novaframe.quartz;

import io.github.novaframe.CodeException;
import io.github.novaframe.config.NovaframeCode;
import io.github.novaframe.web.BaseController;
import io.github.novaframe.web.error.ErrorVM;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by Locki on 9/11/2017.
 */
@Slf4j
@RestController
@RequestMapping(value = "/api/quartz")
public class QuartzJobController extends BaseController {

    final QuartzJobService jobService;

    public QuartzJobController(QuartzJobService jobService) {
        this.jobService = jobService;
    }

    @ApiOperation(value="获取所有任务", notes="获取所有任务")
    @GetMapping(value = "/getAllJobs")
    public List<QuartzJobModel> getAllJobs() {
        try {
            return jobService.getAllJobs();
        } catch (Exception e) {
            log.error("quartz error: {}", e.getMessage());
            throw new CodeException(NovaframeCode.QUARTZ_ERROR);
        }
    }

    @ApiOperation(value="添加任务", notes="添加任务")
    @PostMapping(value = "/addJob")
    public ErrorVM addJob(@RequestBody @Valid QuartzJobModel quartzJobModel) {
        try {
            jobService.addJob(quartzJobModel);
            return ErrorVM.success();
        } catch (SchedulerException e) {
            log.error("quartz error: {}", e.getMessage());
            throw new CodeException(NovaframeCode.QUARTZ_ERROR);
        }
    }

    @ApiOperation(value="暂停任务", notes="暂停任务")
    @PostMapping(value = "/pauseJob", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ErrorVM pauseJob(String jobName, String jobGroup) {
        try {
            jobService.pauseJob(jobName, jobGroup);
            return ErrorVM.success();
        } catch (Exception e) {
            log.error("quartz error: {}", e.getMessage());
            throw new CodeException(NovaframeCode.QUARTZ_ERROR);
        }
    }

    @ApiOperation(value="恢复任务", notes="恢复任务")
    @PostMapping(value = "/resumeJob", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ErrorVM resumeJob(String jobName, String jobGroup) {
        try {
            jobService.resumeJob(jobName, jobGroup);
            return ErrorVM.success();
        } catch (Exception e) {
            log.error("quartz error: {}", e.getMessage());
            throw new CodeException(NovaframeCode.QUARTZ_ERROR);
        }
    }

    @ApiOperation(value="删除任务", notes="删除任务")
    @PostMapping(value = "/deleteJob", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ErrorVM deleteJob(String jobName, String jobGroup) {
        try {
            jobService.deleteJob(jobName, jobGroup);
            return ErrorVM.success();
        } catch (Exception e) {
            log.error("quartz error: {}", e.getMessage());
            throw new CodeException(NovaframeCode.QUARTZ_ERROR);
        }
    }

    @ApiOperation(value="立即执行任务", notes="立即执行任务")
    @PostMapping(value = "/runJob", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ErrorVM runJob(String jobName, String jobGroup) {
        try {
            jobService.runJob(jobName, jobGroup);
            return ErrorVM.success();
        } catch (Exception e) {
            log.error("quartz error: {}", e.getMessage());
            throw new CodeException(NovaframeCode.QUARTZ_ERROR);
        }
    }
}
